package com.koron.sinoprobe.web.engineering.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.interceptor.OrderByInterceptor;
import com.koron.sinoprobe.config.Constants;
import com.koron.sinoprobe.web.engineering.bean.dto.SpWarrantyAcceptDTO;
import com.koron.sinoprobe.web.engineering.bean.po.SpEstablish;
import com.koron.sinoprobe.web.engineering.bean.po.SpWarrantyAccept;
import com.koron.sinoprobe.web.engineering.bean.query.SpWarrantyAcceptQuery;
import com.koron.sinoprobe.web.engineering.bean.vo.SpWarrantyAcceptVO;
import com.koron.sinoprobe.web.engineering.mapper.SpWarrantyAcceptMapper;
import com.koron.sinoprobe.web.engineering.service.SpEstablishService;
import com.koron.sinoprobe.web.engineering.service.SpWarrantyAcceptService;
import com.koron.sinoprobe.web.engineering.service.SpWarrantyConstructionService;
import com.koron.sinoprobe.web.ledger.bean.entity.SpProject;
import com.koron.sinoprobe.web.ledger.service.SpProjectService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 质保期满验收(SpWarrantyAccept)表服务实现类
 *
 * @author jzp
 * @since 2022-03-03 09:21:34
 */
@Service
public class SpWarrantyAcceptServiceImpl implements SpWarrantyAcceptService {

    @Autowired
    private SpWarrantyConstructionService spWarrantyConstructionService;

    @Autowired
    private SpEstablishService spEstablishService;

    @Autowired
    private SpProjectService spProjectService;

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public SpWarrantyAcceptDTO getById(SessionFactory factory, String id) {
        SpWarrantyAcceptMapper mapper = factory.getMapper(SpWarrantyAcceptMapper.class);
        SpWarrantyAcceptDTO spWarrantyAcceptDTO = mapper.selectById(id);
        SpEstablish spEstablish = spEstablishService.getByProjectId(factory, spWarrantyAcceptDTO.getProjectId());
        spWarrantyAcceptDTO.setSpEstablish(spEstablish);
        SpProject spProject = spProjectService.getById(factory, spWarrantyAcceptDTO.getProjectId());
        spWarrantyAcceptDTO.setSpProject(spProject);
        return spWarrantyAcceptDTO;
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<SpWarrantyAcceptVO> list(SessionFactory factory, SpWarrantyAcceptQuery query) {
        SpWarrantyAcceptMapper mapper = factory.getMapper(SpWarrantyAcceptMapper.class);
        return mapper.selectList(query);
    }

    @Override
    @TaskAnnotation("page")
    public PageInfo<SpWarrantyAcceptVO> page(SessionFactory factory, PageQuery pageQuery, SpWarrantyAcceptQuery query) {
        OrderByInterceptor.setOrderBy(pageQuery.getOrders());            // 设置排序，若不设置排序，插件会因为没有排序字段而直接跳过，不会影响分页插件
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize());  //设置分页
        List<SpWarrantyAcceptVO> spWarrantyAcceptList = list(factory, query);
        return new PageInfo<>(spWarrantyAcceptList);
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("saveOrUpdate")
    public boolean saveOrUpdate(SessionFactory factory, SpWarrantyAccept spWarrantyAccept) {
        // 默认草稿
        if (StringUtils.isEmpty(spWarrantyAccept.getStatus())) {
            spWarrantyAccept.setStatus(Constants.AppceptStatus.DRAFT);
        }
        SpWarrantyAcceptMapper mapper = factory.getMapper(SpWarrantyAcceptMapper.class);
        return mapper.insertOrUpdate(spWarrantyAccept) > 0;
    }

    @Override
    @TaskAnnotation("saveOrUpdateDto")
    public boolean saveOrUpdateDto(SessionFactory factory, SpWarrantyAcceptDTO spWarrantyAcceptDTO) {
        // 1. 保存主题信息
        this.saveOrUpdate(factory, spWarrantyAcceptDTO);

        // 2. 保存项目信息
        spWarrantyConstructionService.removeByWarrantyId(factory, spWarrantyAcceptDTO.getId());
        spWarrantyAcceptDTO.getConstructionList().forEach(e -> e.setWarrantyId(spWarrantyAcceptDTO.getId()));
        spWarrantyConstructionService.saveBatch(factory, spWarrantyAcceptDTO.getConstructionList());
        return true;
    }


    @Override
    @TaskAnnotation("end")
    public boolean end(SessionFactory factory, String id) {
        // 1. 更新状态
        SpWarrantyAccept spWarrantyAccept = new SpWarrantyAccept();
        spWarrantyAccept.setId(id);
        spWarrantyAccept.setStatus(Constants.AppceptStatus.APPROVED);
        return update(factory, spWarrantyAccept);
    }

    @Override
    @TaskAnnotation("submit")
    public boolean submit(SessionFactory factory, SpWarrantyAccept spWarrantyAccept) {
        spWarrantyAccept.setStatus(Constants.AppceptStatus.WAIT_REVIEW);
        return saveOrUpdate(factory, spWarrantyAccept);
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, SpWarrantyAccept spWarrantyAccept) {
        SpWarrantyAcceptMapper mapper = factory.getMapper(SpWarrantyAcceptMapper.class);
        return mapper.update(spWarrantyAccept) > 0;
    }

    @Override
    @TaskAnnotation("updateStatus")
    public boolean updateStatus(SessionFactory factory, String id, String status) {
        SpWarrantyAccept spWarrantyAccept = new SpWarrantyAccept();
        spWarrantyAccept.setId(id);
        spWarrantyAccept.setStatus(status);
        return update(factory, spWarrantyAccept);
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        SpWarrantyAcceptMapper mapper = factory.getMapper(SpWarrantyAcceptMapper.class);
        return mapper.hidden(id) > 0;
    }
}
